---
title: "Supreme Court"
output:
  word_document: default
  html_document: default
---

```{r}
library(rio) 
library(ggplot2) 
library(knitr) 
library(tidyverse)
library(ggeffects) 
library(uwo4419)
library(DAMisc)
library(dplyr)
library(stargazer)
library(Hmisc)
```


*Data*
```{r}
library("readxl")
SCC<- read_excel("SCC Database.xlsx")
```

*Weighted Linear Regression*
```{r}
#Criminal Cases

PrimeMinisterCrim <- lm(CriminalLiberalScore ~ PM, data = SCC, weights = TotalCriminalCases)
summary(PrimeMinisterCrim)

GlobeandMailCrim <- lm(CriminalLiberalScore ~ GlobeandMail, data = SCC, weights = TotalCriminalCases)
summary(GlobeandMailCrim)

OttawaCitizenCrim <- lm(CriminalLiberalScore ~ OttawaCitizen, data = SCC, weights = TotalCriminalCases)
summary(OttawaCitizenCrim)

RegionalCrim <- lm(CriminalLiberalScore ~ Regional, data = SCC, weights = TotalCriminalCases)
summary(RegionalCrim)

CumulativeCrim <- lm(CriminalLiberalScore ~ Cumulative, data = SCC, weights = TotalCriminalCases)
summary(CumulativeCrim)

```


```{r}
#Civil Liberties Cases

PrimeMinisterCiv <- lm(CivilLiberalScore ~ PM, data = SCC, weights = TotalCivLibCases)
summary(PrimeMinisterCiv)

GlobeandMailCiv <- lm(CivilLiberalScore ~ GlobeandMail, data = SCC, weights = TotalCivLibCases)
summary(GlobeandMailCiv)

OttawaCitizenCiv <- lm(CivilLiberalScore ~ OttawaCitizen, data = SCC, weights = TotalCivLibCases)
summary(OttawaCitizenCiv)

RegionalCiv <- lm(CivilLiberalScore ~ Regional, data = SCC, weights = TotalCivLibCases)
summary(RegionalCiv)

CumulativeCiv <- lm(CivilLiberalScore ~ Cumulative, data = SCC, weights = TotalCivLibCases)
summary(CumulativeCiv)
```


```{r}
#Economic Cases

PrimeMinisterEcon <- lm(EconLibScore ~ PM, data = SCC, weights = TotalEconCases)
summary(PrimeMinisterEcon)

GlobeandMailEcon <- lm(EconLibScore ~ GlobeandMail, data = SCC, weights = TotalEconCases)
summary(GlobeandMailEcon)

OttawaCitizenEcon <- lm(EconLibScore ~ OttawaCitizen, data = SCC, weights = TotalEconCases)
summary(OttawaCitizenEcon)

RegionalEcon <- lm(EconLibScore ~ Regional, data = SCC, weights = TotalEconCases)
summary(RegionalEcon)

CumulativeEcon <- lm(EconLibScore ~ Cumulative, data = SCC, weights = TotalEconCases)
summary(CumulativeEcon)

```


*Combined Data*
```{r}
songer <- rio::import("CombinedData.xlsx")
songer_w <- songer %>%
  filter(stat=="b" | stat=="se") %>% 
  pivot_wider(names_from=stat, values_from=value)
```

```{r}
#Two-tailed tests
songer_w$lwr <- songer_w$b - (songer_w$se * 1.96)
songer_w$upr <- songer_w$b + (songer_w$se * 1.96)
songer_w$sig <- ifelse(songer_w$b / songer_w$se > 1.96, "Significant", "Not Significant")
songer_w$period <- factor(songer_w$period, levels = c("2005-2021", "1984-2002"))

library(ggstance)
pdf("p1.pdf")
p1 <- ggplot(songer_w) +
  aes(y=source, x=b, xmin=lwr, xmax=upr, colour=period, shape = sig) +
  facet_wrap(~area) +
  geom_pointrangeh(position=position_dodge(.4)) +
  geom_vline(xintercept=0, linetype="dotted") +
  scale_shape_manual(values=c(1,16)) +
  theme_bw() +
  scale_color_manual(values=c('Grey', 'Black')) +
  scale_y_discrete(labels = c("Cumulative \nnewspaper index",
                              "Globe and Mail \nideology",
                              "Ottawa Citizen \nideology",
                              "Party of \nPrime Minister")) +
  #theme(axis.text.x = element_text(angle=45, hjust=1)) +
  guides(colour = guide_legend(reverse=TRUE)) + 
  labs(y = "Source of Ideology Measure", 
       x = "Effect of being classified as liberal on the \nprobability of supporting a liberal position (in % points)", 
       colour = "Period", 
       shape = "Signficant at the .05 level\n(two-tailed)")
print(p1)
dev.off()
```


```{r}
#One-tailed tests
songer_w$lwr1t <- songer_w$b - (songer_w$se * 1.645)
songer_w$upr1t <- songer_w$b
songer_w$sig <- ifelse(songer_w$b / songer_w$se > 1.465, "Significant", "Not Significant")
songer_w$period <- factor(songer_w$period, levels = c("2005-2021", "1984-2002"))

pdf("p2.pdf")
p2 <-ggplot(songer_w) +
  aes(x=b, y=source, xmin=lwr1t, xmax=upr1t, colour=period, shape = sig) +
  facet_wrap(~area) +
  geom_pointrangeh(position=position_dodge(.4)) +
  geom_vline(xintercept=0, linetype="dotted") +
  scale_shape_manual(values=c(1,16)) +
  theme_bw() +
  scale_color_manual(values=c('Grey', 'Black')) +
  scale_y_discrete(labels = c("Cumulative \nnewspaper index",
                              "Globe and Mail \nideology",
                              "Ottawa Citizen \nideology",
                              "Party of \nPrime Minister")) +
  #theme(axis.text.x = element_text(angle=45, hjust=1)) +
  guides(colour = guide_legend(reverse=TRUE)) + 
  labs(y = "Source of Ideology Measure", 
       x = "Effect of being classified as liberal on the \nprobability of supporting a liberal position (in % points)",  
       colour = "Period", 
       shape = "Signficant at the .05 level\n(one-tailed)")
print(p2)
dev.off()
```

